/*
 * Widget.h
 *
 *  Created on: Sep 5, 2011
 *      Author: Patrick
 */

#ifndef WIDGET_H_
#define WIDGET_H_

#include "MediaCenter.h"
#include "Repository.h"
#include <vector>

extern "C" {
#include "video/Video.h"
}

using namespace std;

#define MCC_POSITION_RELATIVE 1
#define MCC_POSITION_ABSOLUTE 2

class MCComponent {

	private:

		vector<MCComponent*> children;
		unsigned int childrenSize;

	public:

		MCComponent* parent;
		float x, y, w, h;
		short position;

		MCComponent() {
			this->childrenSize = 0;
		}

		virtual void draw() {
			for (unsigned int i = 0; i < this->childrenSize; i++) {
				this->children[i]->draw();
			}
		}

		MCComponent addChild(MCComponent* child) {
			child->parent = this;
			this->children.push_back(child);
			this->childrenSize+=1;
			return *child;
		}

};

class MCSpriteComponent: public MCComponent {

	protected:

		char* textureName;
		GLuint textureID;

	public:

		void setTexture(char* name, char* path) {
			this->textureName = name;
			this->textureID = MCTextureCreate(name, path);
			glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &this->w);
			glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &this->h);
		}

		void setTexture(GLuint id) {
			this->textureID = id;
		}

		virtual void draw() {
			glBindTexture(GL_TEXTURE_2D, this->textureID);
			glBegin(GL_QUADS);
			glTexCoord2d(0, 0);
			glVertex2f(x, y);
			glTexCoord2d(1, 0);
			glVertex2f(x + w, y);
			glTexCoord2d(1, 1);
			glVertex2f(x + w, y + h);
			glTexCoord2d(0, 1);
			glVertex2f(x, y + h);
			glEnd();
		}

};

class MCVideoComponent: public MCSpriteComponent {

	public:

		void setTexture() {
			this->textureName = (char*)MC_TEX_VIDEO;
			this->textureID = MCTextureCreate((char*)MC_TEX_VIDEO);
		}

		void draw() {
			// Streaming the texture will also bind it to GL_TEXTURE_2D
			glBindTexture(GL_TEXTURE_2D, MCVideoStreamTexture(this->textureID));
			glColor3f(1, 1, 1);
			glBegin(GL_QUADS);
			glTexCoord2d(0, 1);
			glVertex2f(x, y);
			glTexCoord2d(1, 1);
			glVertex2f(x + w, y);
			glTexCoord2d(1, 0);
			glVertex2f(x + w, y + h);
			glTexCoord2d(0, 0);
			glVertex2f(x, y + h);
			glEnd();
		}

};

#endif /* WIDGET_H_ */
